package www.nfin.ya.entity.excel;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RowEntity {
    private short foregroundColor;
    @Builder.Default
    private RowType rowType = RowType.value;
    @Builder.Default
    List<CellEntity> cells = new ArrayList<>();
    public boolean writeRow(XSSFWorkbook workbook, XSSFSheet sheet,boolean firstRow){
        int lastRowNum = sheet.getLastRowNum();
        XSSFRow row = sheet.createRow(firstRow ? 0:lastRowNum+1 );

        CellStyle cellStyle = workbook.createCellStyle();


        if(foregroundColor != 0){
            cellStyle.setFillForegroundColor(foregroundColor);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }

        for (CellEntity cell : cells) {
            cell.writeCell(workbook,sheet,row);
        }

        row.setRowStyle(cellStyle);
        return true;
    }
    public CellEntity createCell(){
        CellEntity cellEntity = new CellEntity();
         cells.add(cellEntity);
        return cellEntity;
    }
    public void addCell(CellEntity cellEntity){
        cells.add(cellEntity);
    }
    public enum RowType{
        column,value
    }
}
